package com.yanqu.road.dao.impl.activity.luckyshop;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.luckyshop.LuckyShopUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class LuckyShopUserDataDaoImpl extends TempDataObjectDao<LuckyShopUserData> {

    public static String getLuckyShopUserDataSql = "select * from `t_u_activity_lucky_shop_data` where `activityId`=? and `userId`=?";

    public LuckyShopUserData getUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getLuckyShopUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertLuckyShopUserDataSql = "insert into `t_u_activity_lucky_shop_data`(activityId,userId,rewardIdx,drawTimes,isReward) values (?,?,?,?,?)";

    @Override
    protected boolean addData(LuckyShopUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRewardIdx()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawTimes()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReward()));
        if (getDb().executeNoneQuery(insertLuckyShopUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String updateLuckyShopUserDataSql = "update `t_u_activity_lucky_shop_data` set `rewardIdx`=?,`drawTimes`=?,`isReward`=? where `activityId`=? and `userId`=?";

    @Override
    protected boolean updateData(LuckyShopUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getRewardIdx()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrawTimes()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateLuckyShopUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected LuckyShopUserData parseData(ResultSet rs) throws SQLException {
        LuckyShopUserData data = new LuckyShopUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setRewardIdx(rs.getInt("rewardIdx"));
        data.setDrawTimes(rs.getInt("drawTimes"));
        data.setReward(rs.getBoolean("isReward"));
        return data;
    }
}
